import React, { Component ,useState} from 'react'
import "../static/css/home.css"
import {
    Form,
    Select,
    Input,
    InputNumber,
    Switch,
    Radio,
    Slider,
    Button,
    Upload,
    Rate,
    Checkbox,
    Row,
    Col,
    message
} from 'antd';
import {LoadingOutlined, PlusOutlined  } from '@ant-design/icons';
//富文本编辑器
import  EditorVan from '../utils/EditorVan.js'


function getBase64(img, callback) {
    const reader = new FileReader();
    reader.addEventListener('load', () => callback(reader.result));
    reader.readAsDataURL(img);
}

function beforeUpload(file) {
    const isJpgOrPng = file.type === 'image/jpeg' || file.type === 'image/png';
    if (!isJpgOrPng) {
        message.error('You can only upload JPG/PNG file!');
    }
    const isLt2M = file.size / 1024 / 1024 < 2;
    if (!isLt2M) {
        message.error('Image must smaller than 2MB!');
    }
    return isJpgOrPng && isLt2M;
}
export default  class address extends Component{
    state = {
        value: 1,
        loading: false,
        editorState:"<div>测试</div>"
    };
    onChange = e => {
        console.log('radio checked', e.target.value);
        this.setState({
            value: e.target.value,
        });
    };
    handleChange = info => {
        if (info.file.status === 'uploading') {
            this.setState({ loading: true });
            return;
        }
        if (info.file.status === 'done') {
            // Get this url from response in real world.
            getBase64(info.file.originFileObj, imageUrl =>
                this.setState({
                    imageUrl,
                    loading: false,
                }),
            );
        }
    };
    render(){
        console.log(this.state.editorState)  //文本编辑器内容打印
        const { Option } = Select;
        const formItemLayout = {
            labelCol: { span: 4 },
            wrapperCol: { span: 20 },
        };

        const normFile = e => {
            console.log('Upload event:', e);
            if (Array.isArray(e)) {
                return e;
            }
            return e && e.fileList;
        };
        const onFinish = values => {
            console.log('Received values of form: ', values);
        };
        const uploadButton = (
            <div>
                {this.state.loading ? <LoadingOutlined /> : <PlusOutlined />}
                <div className="ant-upload-text">Upload</div>
            </div>
        );
        const { imageUrl } = this.state;
        return (
            <div className="el-card">
                <h3 className="h3tit">商品介绍</h3>
                <div>
                    {/*<Demo onChange={this.onChange} value={this.state.value} />*/}
                    <Form
                        className="goods"
                        name="validate_other"
                        {...formItemLayout}
                        onFinish={onFinish}
                        initialValues={{
                            ['input-number']: 3,
                            ['checkbox-group']: ['A', 'B'],
                            rate: 12.5,
                        }}
                    >
                        <Form.Item label="商品编号">
                            <Input placeholder="请输入商品编号"/>
                        </Form.Item>
                        <Form.Item label="商品名称">
                            <Input placeholder="请输入商品名称"/>
                        </Form.Item>
                        <Form.Item label="市场售价">
                            <Input addonAfter={"元"} placeholder="0.00" />
                        </Form.Item>
                        <Form.Item label="是否新品">
                            <Radio.Group onChange={this.onChange} value={this.state.value}>
                                <Radio value={1}>新品</Radio>
                                <Radio value={2}>非新品</Radio>
                            </Radio.Group>
                        </Form.Item>
                        <Form.Item label="是否热卖">
                            <Radio.Group onChange={this.onChange} value={this.state.value}>
                                <Radio value={1}>普通</Radio>
                                <Radio value={2}>热卖</Radio>
                            </Radio.Group>
                        </Form.Item>
                        <Form.Item label="是否在售">
                            <Radio.Group onChange={this.onChange} value={this.state.value}>
                                <Radio value={1}>在售</Radio>
                                <Radio value={2}>未售</Radio>
                            </Radio.Group>
                        </Form.Item>
                        <Form.Item label="商品图片">
                            <Upload
                                name="avatar"
                                listType="picture-card"
                                className="avatar-uploader"
                                showUploadList={false}
                                action="https://www.mocky.io/v2/5cc8019d300000980a055e76"
                                beforeUpload={beforeUpload}
                                onChange={this.handleChange}
                            >
                                {imageUrl ? <img src={imageUrl} alt="avatar" style={{ width: '100%' }} /> : uploadButton}
                            </Upload>
                        </Form.Item>
                        <Form.Item label="宣传画廊">
                            <Upload
                                name="avatar"
                                listType="picture-card"
                                className="avatar-uploader"
                                showUploadList={false}
                                action="https://www.mocky.io/v2/5cc8019d300000980a055e76"
                                beforeUpload={beforeUpload}
                                onChange={this.handleChange}
                            >
                                {imageUrl ? <img src={imageUrl} alt="avatar" style={{ width: '100%' }} /> : uploadButton}
                            </Upload>
                        </Form.Item>
                        <Form.Item label="商品单位">
                            <Input placeholder="件/个/盒"/>
                        </Form.Item>
                        <Form.Item
                            name="select"
                            label="所属分类"
                            hasFeedback
                            rules={[{ required: true, message: 'Please select your country!' }]}
                        >
                            <Select placeholder="请选择">
                                <Option value="china">1</Option>
                                <Option value="usa">2</Option>
                            </Select>
                        </Form.Item>
                        <Form.Item
                            name="select"
                            label="所属品牌商"
                            hasFeedback
                            rules={[{ required: true, message: 'Please select your country!' }]}
                        >
                            <Select placeholder="请选择">
                                <Option value="china">1</Option>
                                <Option value="usa">2</Option>
                            </Select>
                        </Form.Item >

                        <Form.Item
                            name="select-multiple"
                            label="关键字"
                            rules={[{ required: true, message: 'Please select your favourite colors!', type: 'array' }]}
                        >
                            <Select mode="multiple" placeholder="请选择关键字">
                                <Option value="red">1</Option>
                                <Option value="green">2</Option>
                                <Option value="blue">3</Option>
                            </Select>
                        </Form.Item>
                        <Form.Item  label="商品详情介绍">
                            <div style={{border:"1px solid #d9d9d9"}}>
                                <EditorVan editorState={this.state.editorState} transformHTML={(editorState) => this.setState({ editorState})} />
                            </div>
                        </Form.Item>
                        <Form.Item wrapperCol={{ span: 12, offset: 4 }}>
                            <Button type="primary" htmlType="submit">
                                提交
                            </Button>
                        </Form.Item>
                    </Form>
                </div>


            </div>
        )
    }
}
